package com.zxxy.gulimall.auth.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.zxxy.common.utils.HttpUtils;
import com.zxxy.common.utils.R;
import com.zxxy.gulimall.auth.feign.MemberFeignService;
import com.zxxy.common.vo.MemberRespVo;
import com.zxxy.gulimall.auth.vo.SocialUser;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class OAuth2Controller {

    @Autowired
    MemberFeignService memberFeignService;

    @Autowired
    RedisTemplate redisTemplate;



    // 社交登录成功回调
    @GetMapping("/oauth2.0/weibo/success")
    public String weibo(@RequestParam("code") String code, HttpServletResponse res, HttpSession session) throws Exception {


        System.out.println("00000000000000000000000000");
        Map<String,String> map = new HashMap<>();
        Map<String,String> map1 = new HashMap<>();
        Map<String,String> map2 = new HashMap<>();
        map.put("client_id","246517522");
        map.put("client_secret","bafc42de37d1bd88f8cb94a8575594a0");
        map.put("grant_type","authorization_code");
        map.put("redirect_uri","http://auth.gulimall.com/oauth2.0/weibo/success");
        map.put("code",code);
        // 根据code换取accessToken
        HttpResponse response = HttpUtils.doPost("https://api.weibo.com", "/oauth2/access_token", "post", map1, map2, map);
        System.out.println("----------"+response.getStatusLine().getStatusCode());
        // 处理
        if (response.getStatusLine().getStatusCode()==200){
            // 获取到了  accessToken
            String json = EntityUtils.toString(response.getEntity());
            SocialUser socialUser = JSON.parseObject(json, SocialUser.class);
            System.out.println("----"+socialUser);

            R oauthlogin = memberFeignService.oauthlogin(socialUser);
            if (oauthlogin.getCode() == 0){
                MemberRespVo data = oauthlogin.getData(new TypeReference<MemberRespVo>() {
                });
                System.out.println("登录成功------用户信息"+data);
                // 1、第一次使用session；命令浏览器保存卡号、JSESSIONID这个cookie
                // 以后浏览器访问那个网站就会带上这个网站的cookie
                // 子域之间： gulimall.com  auth.gulimall.com  order.gulimall.com
                // 发卡的时候（指定域名为父域名），即使是子域系统发的卡，也能让父域直接使用

                // TODO 1、默认发的令牌。session=xxx，，作用域：当前域 ：（解决子域的session共享问题）
                // TODO 2、使用JSON的序列化方式来序列化对象数据到redis中
                session.setAttribute("loginUser",data);
                //res.addCookie();
                return "redirect:http://gulimall.com";
            }else {
                return "redirect:http://auth.gulimall.com/login.html";
            }

        }else{
            return "redirect:http://auth.gulimall.com/login.html";
        }

        // 登录成功就调回首页
        // return "redirect:http://auth.gulimall.com/login.html";
    }


}
